﻿using System.Collections.Generic;
using System.Net;
using System.Text;

namespace AnesSystem.Udp
{

    public class MsgHelper
    {
        public static string MSG = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                                    "<!-- type，区分手麻还是icu-->\n" +
                                        "<root type=\"Anes\">\n" +
                                            "<!-- 功能id，对应2.1服务列表-->\n" +
                                            "<MethodId>{0}</MethodId>\n" +
                                            "<!--自定义内容-->\n" +
                                            "<CustomContent></CustomContent>\n" +
                                            "<!--业务id，如果手术相关的接口就是手术申请id集合-->\n" +
                                            "<businessIds>\n {1}" + "\n" +
                                            "</businessIds>\n" + 
                                            "<IP>{2}</IP>" +
                                         "</root>";


        public static string getXML(string methodId, IList<string> hisIdList)
        {
            string xml = MSG;
            string methodid = methodId;
            StringBuilder buffer = new StringBuilder("");
            if (hisIdList != null)
            {

                if (hisIdList.Count > 0)
                {
                    for (int i = 0; i < hisIdList.Count; i++)
                    {
                        buffer.Append("<id>" + hisIdList[i] + "</id>");
                    }
                }
                else
                {
                    buffer.Append("<id>" + "" + "</id>");
                }
            }
            return string.Format(MSG, methodId, buffer.ToString(),UpdateIPs());
        }


        //判断是否是IPV4
        private static bool IsValidIP(string ip)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
            {
                string[] ips = ip.Split('.');
                if (ips.Length == 4 || ips.Length == 6)
                {
                    if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            else
                return false;
        }
        public static string UpdateIPs()
        {
            IPAddress[] ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            string ips = "";
            foreach (IPAddress item in ip)
            {
                if (IsValidIP(item.ToString()))
                {
                    //偷懒 返回第一个IP
                    return item.ToString();
                }
            }
            return "";
            //DataManager.Instance.CurRoom.Ips = ips;
        }

    }
}